package com.example.networklab.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.networklab.common.ResultResponse;
import com.example.networklab.domain.query.PageQuery;
import com.example.networklab.domain.req.SubmitTaskReq;
import com.example.networklab.service.ITaskHistoryService;
import com.example.networklab.service.ITaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*" )
@RestController
@RequiredArgsConstructor
@SaCheckLogin
@SaCheckRole("student" )
public class StudentController {
    private final ITaskService taskService;
    private final ITaskHistoryService taskHistoryService;

    //分页查询该学生的作业
    @GetMapping("/stu/task/list" )
    public ResultResponse getTaskList(PageQuery pageQuery) {
        return ResultResponse.success(taskService.stuGetTaskList(pageQuery));
    }

    //学生提交作业
    @PostMapping("/stu/task/submit" )
    public ResultResponse submitTask(@Validated @RequestBody SubmitTaskReq submitTaskReq) {
        taskHistoryService.submitTask(submitTaskReq);
        return ResultResponse.success();
    }

    //查看作业说明
    @GetMapping("/stu/task/spec/{taskId}")
    public ResultResponse getTaskSpec(@PathVariable("taskId") Long taskId) {
        return ResultResponse.success(taskService.getTaskSpec(taskId));
    }

    //分页查看提交的历史记录
    @GetMapping("/stu/task/history/{taskId}")
    public ResultResponse getTaskHistory(@PathVariable("taskId") Long taskId, PageQuery pageQuery) {
        return ResultResponse.success(taskHistoryService.stuGetTaskHistory(taskId, pageQuery));
    }

    //查看提交的配置信息
    @GetMapping("/stu/task/history/config/{taskHistoryId}")
    public ResultResponse getTaskHistoryConfig(@PathVariable("taskHistoryId") Long taskHistoryId) {
        return ResultResponse.success(taskHistoryService.getTaskHistoryConfig(taskHistoryId));
    }

    //查询作业的最高分数
    @GetMapping("/stu/task/grade/{taskId}")
    public ResultResponse getTaskGrade(@PathVariable("taskId") Long taskId) {
        return ResultResponse.success(taskHistoryService.getTaskGrade(taskId));
    }
}
